<template>
    <div class="container">
        <h1 class="text-center">Создание Экспертизы</h1>
        <b-form @submit.prevent="createExpertise">
            <b-form-group
                id="fieldset-0"
                label="Введите идентификатор эксперта отвечающего за экспертизу"
                label-for="input-expert"
            >
                <div>
                    <b-form-input @change="getExpert" type="number" required id="input-expert" v-model="select_expert"></b-form-input>
                    <div class="mt-3">Вы выбрали: <strong>{{ selected_expert }}</strong></div>
                </div>
            </b-form-group>
            <b-form-group
                id="fieldset-0"
                label="Введите идентификатор заказчика экспертизы"
                label-for="input-customer"
            >
                <div>
                    <b-form-input @change="getCustomer" type="number" required id="input-customer" v-model="select_customer"></b-form-input>
                    <div class="mt-3">Вы выбрали: <strong>{{ selected_customer }}</strong></div>
                </div>
            </b-form-group>
            <b-form-group
                id="fieldset-1"
                label="Дата получения материалов:"
                label-for="input-1"
            >
                <b-form-input
                    id="input-1"
                    v-model="dateOfMaterial"
                    required
                    type="date"
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-2"
                label="Дата начала экспертизы:"
                label-for="input-2"
            >
                <b-form-input
                    id="input-2"
                    v-model="startDate"
                    required
                    type="date"
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-3"
                label="Дата окончания экспертизы:"
                label-for="input-3"
            >
                <b-form-input
                    id="input-3"
                    v-model="endDate"
                    required
                    type="date"
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-4"
                label="Дата выдачи заключения:"
                label-for="input-4"
            >
                <b-form-input
                    id="input-4"
                    v-model="detentionDate"
                    required
                    type="date"
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-5"
                label="Стоимост экспертизы (руб.):"
                label-for="input-5"
            >
                <b-form-input
                    id="input-5"
                    v-model="costExpertise"
                    required
                    type="number"
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-6"
                label="Вид экспертизы:"
                label-for="input-6"
            >
                <b-form-input
                    id="input-6"
                    v-model="typeExpertise"
                    required
                    type="text"
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-7"
                label="Колличество исследуемых объектов:"
                label-for="input-7"
            >
                <b-form-input
                    id="input-7"
                    type="number"
                    required
                    v-model="objConclusion"
                ></b-form-input>
            </b-form-group>

            <b-form-group id="fieldset-8" label="Виды выводов:" label-for="input-8">
                <b-form-input
                    id="input-8"
                    v-model="typeConclusion"
                    required
                    type="text"
                ></b-form-input>
            </b-form-group>

            <b-form-group
                id="fieldset-9"
                label="Заключение экспетра:"
                label-for="input-9"
            >
                <b-form-textarea
                    id="input-9"
                    type="text"
                    required
                    v-model="textConclusion"
                ></b-form-textarea>
            </b-form-group>

            <b-button type="submit" variant="primary">Создать</b-button>
        </b-form>
    </div>
</template>

<script>
export default {
    data() {
        return {
            selected_expert: "",
            selected_customer: "",
            select_expert: "",
            select_customer: "",
            dateOfMaterial: "",
            startDate: "",
            endDate: "",
            detentionDate: "",
            costExpertise: "",
            typeExpertise: "",
            objConclusion: "",
            typeConclusion: "",
            textConclusion: "",
            resp: [],
            errors: []
        };
    },
    methods: {
        async createExpertise() {
            await this.$http.post("/expertise/create", {
                "date_of_receipt_of_materials": this.dateOfMaterial,
                "research_start_date": this.startDate,
                "research_end_date": this.endDate,
                "expert_detention_date": this.detentionDate,
                "cost_of_expertise": this.costExpertise,
                "type_expertise": this.typeExpertise,
                "number_of_objects_research": this.objConclusion,
                "types_of_conclusions": this.typeConclusion,
                "text_of_conclusion": this.textConclusion,
                "expert_id": this.select_expert,
                "customer_id": this.select_customer
            })
            .then(response => {
                console.log(response)
                this.resp = response
                this.$router.push({path: "/expertise"}) 
            })
            .catch(e => {
                this.resp = e
                this.errors.push(e)
            })

        },

        async getExpert() {
            await this.$http.get("/expert/" + this.select_expert)
                .then(response => {
                    this.selected_expert = response.data.last_name + " " + response.data.first_name + " " + response.data.patronymic
                })
                .catch(e => {
                    this.selected_expert = "Эксперт не найден"
                    this.errors.push(e)
                })
        },
        async getCustomer() {
            await this.$http.get("/customer/" + this.select_customer)
                .then(response => {
                    this.selected_customer = response.data.first_name + " " + response.data.last_name + " " + response.data.patronymic
                })
                .catch(e => {
                    this.selected_customer = "Заказчик не найден"
                    this.errors.push(e)
                })
        }
    },

};
</script>
<style>
</style>
